package com.itheima.wisdomnext.mapper;

import com.itheima.wisdomnext.entity.Ban;
import org.apache.ibatis.annotations.*;

@Mapper
public interface BanMapper {

    // 封禁用户
    @Update("UPDATE users SET banned = 1 WHERE userid = #{userId}")
    int updateUserBanned(int userId);

    // 解禁用户
    @Update("UPDATE users SET banned = 0 WHERE userid = #{userId}")
    int updateUserUnbanned(int userId);

    // 减少分类表中对应分类的帖子数量（按用户 ID 封禁帖子）
    @Update("UPDATE categories c " +
            "JOIN posts p ON c.id = p.category_id " +  // 将分类表与帖子表通过 category_id 关联
            "SET c.post_count = c.post_count - (" +
            "    SELECT COUNT(*) FROM posts p2 " +  // 计算该分类下属于此用户的未封禁帖子数量
            "    WHERE p2.userid = #{userId} AND p2.banned = 0 AND p2.category_id = c.id" +
            ") " +
            "WHERE c.id IN (" +
            "    SELECT p3.category_id FROM posts p3 " +  // 确保更新的分类是有帖子属于该用户的
            "    WHERE p3.userid = #{userId} AND p3.banned = 0" +
            ")")
    int decreasePostCountByUserId(@Param("userId") int userId);

    // 将用户的所有帖子设置为封禁状态
    @Update("UPDATE posts SET banned = 1 WHERE userid = #{userId}")
    int updatePostBannedByUserId(int userId);

    // 增加分类表中对应分类的帖子数量（按用户 ID 解禁帖子）
    @Update("UPDATE categories c " +
            "JOIN posts p ON c.id = p.category_id " +  // 将分类表与帖子表通过 category_id 关联
            "SET c.post_count = c.post_count + (" +
            "    SELECT COUNT(*) FROM posts p2 " +  // 计算该分类下属于此用户的已封禁帖子数量
            "    WHERE p2.userid = #{userId} AND p2.banned = 1 AND p2.category_id = c.id" +
            ") " +
            "WHERE c.id IN (" +
            "    SELECT p3.category_id FROM posts p3 " +  // 确保更新的分类是有帖子属于该用户的
            "    WHERE p3.userid = #{userId} AND p3.banned = 1" +
            ")")
    int increasePostCountByUserId(@Param("userId") int userId);

    // 将用户的所有帖子设置为解禁状态
    @Update("UPDATE posts SET banned = 0 WHERE userid = #{userId}")
    int updatePostUnbannedByUserId(int userId);


    // 封禁用户发布的所有评论
    @Update("UPDATE comments SET banned = 1 WHERE userid = #{userId}")
    int updateCommentBannedByUserId(int userId);

    // 解禁用户发布的所有评论
    @Update("UPDATE comments SET banned = 0 WHERE userid = #{userId}")
    int updateCommentUnbannedByUserId(int userId);

    // 封禁帖子本身
    @Update("UPDATE posts SET banned = 1 WHERE id = #{postId}")
    int updatePostBanned(int postId);

    // 更新分类的帖子数量
    @Update("UPDATE categories SET post_count = post_count - 1 WHERE id = (SELECT category_id FROM posts WHERE id = #{postId})")
    int updateCategoryun(int postId);

    // 解禁帖子本身
    @Update("UPDATE posts SET banned = 0 WHERE id = #{postId}")
    int updatePostUnbanned(int postId);

    // 更新分类的帖子数量
    @Update("UPDATE categories SET post_count = post_count + 1 WHERE id = (SELECT category_id FROM posts WHERE id = #{postId})")
    int updateCategory(int postId);

    // 封禁帖子下的所有评论
    @Update("UPDATE comments SET banned = 1 WHERE post_id = #{postId}")
    int updateCommentBannedByPostId(int postId);

    // 解禁帖子下的所有评论
    @Update("UPDATE comments SET banned = 0 WHERE post_id = #{postId}")
    int updateCommentUnbannedByPostId(int postId);

    // 封禁单个评论
    @Update("UPDATE comments SET banned = 1 WHERE id = #{commentId}")
    int updateSingleCommentBanned(int commentId);

    // 解禁单个评论
    @Update("UPDATE comments SET banned = 0 WHERE id = #{commentId}")
    int updateSingleCommentUnbanned(int commentId);

    // 插入封禁记录
    @Insert("INSERT INTO bans (user_id, post_id, comment_id, reason) " +
            "VALUES (#{userId}, #{postId}, #{commentId}, #{reason})")
    int insertBan(Ban ban);

    @Update("UPDATE bans SET banned = 0 WHERE " +
            "(user_id = #{userId} OR #{userId} IS NULL) " +
            "AND (post_id = #{postId} OR #{postId} IS NULL) " +
            "AND (comment_id = #{commentId} OR #{commentId} IS NULL)")
    int updateBanRecordToUnbanned(@Param("userId") Integer userId,
                                   @Param("postId") Integer postId,
                                   @Param("commentId") Integer commentId);

    // 查询封禁记录
    @Select("<script>" +
            "SELECT * FROM bans WHERE 1=1 AND banned != 0" +
            "<if test='userId != null'> AND user_id = #{userId} </if>" +
            "<if test='postId != null'> AND post_id = #{postId} </if>" +
            "<if test='commentId != null'> AND comment_id = #{commentId} </if>" +
            "</script>")
    Ban findBanInfo(@Param("userId") Integer userId, @Param("postId") Integer postId, @Param("commentId") Integer commentId);
}
